// PlayerController.hpp
#pragma once
#include "Script.hpp"
#include "Components.hpp"
#include "Property.hpp"
#include "Engine.hpp"

class PlayerController : public Script {
public:
    PlayerController() {
        // 初始化属性值
        m_properties["Speed"] = 100.0f;
        m_properties["Health"] = 100;
        m_properties["PlayerName"] = std::string("Player");
        m_properties["IsInvincible"] = false;
    }
    
    std::vector<PropertyDescriptor> GetProperties() const override {
        return {
            {
                "Speed", "Movement Speed", PropertyType::Float,
                100.0f,  // 默认值
                0.0f,    // 最小值
                500.0f,  // 最大值
                "Controls how fast the player moves"
            },
            {
                "Health", "Health Points", PropertyType::Integer,
                100,     // 默认值
                0,       // 最小值
                200,     // 最大值
                "Player's health points"
            },
            {
                "PlayerName", "Player Name", PropertyType::String,
                std::string("Player"), // 默认值
                PropertyValue{},       // 最小值（不使用）
                PropertyValue{},       // 最大值（不使用）
                "Name of the player character"
            },
            {
                "IsInvincible", "Invincible", PropertyType::Boolean,
                false,   // 默认值
                PropertyValue{}, // 最小值（不使用）
                PropertyValue{}, // 最大值（不使用）
                "If enabled, player cannot take damage"
            }
        };
    }
    
    void OnCreate() override {
        std::cout << "PlayerController created for entity: " 
                  << static_cast<uint32_t>(GetEntity()) << std::endl;
        
        // 从属性初始化成员变量
        m_speed = std::get<float>(GetPropertyValue("Speed"));
        m_health = std::get<int>(GetPropertyValue("Health"));
        m_playerName = std::get<std::string>(GetPropertyValue("PlayerName"));
        m_isInvincible = std::get<bool>(GetPropertyValue("IsInvincible"));
    }
    
    void OnUpdate(float deltaTime) override {

        // 获取实体和注册表
        auto entity = GetEntity();
        auto& registry = GetEngine()->getRegistry();
        
        // 检查是否有位置组件
        if (registry.all_of<Position>(entity)) {
            auto& position = registry.get<Position>(entity);
            
            // 使用属性值
            m_speed = std::get<float>(GetPropertyValue("Speed"));
            
            // 简单的移动逻辑
            position.x += m_speed * deltaTime;
            
            // 边界检查
            if (position.x > 800) position.x = 0;
        }
    }
    
    void OnDestroy() override {
        std::cout << "PlayerController destroyed" << std::endl;
    }
    
private:
    float m_speed;
    int m_health;
    std::string m_playerName;
    bool m_isInvincible;
};